package com.zzy.demo.config;

import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.zzy.demo.config.interceptor.DataInterceptor;

/**
 * 
 * @author guokaige
 *
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
	
	/**
	 * 添加json转换
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		/**
		 * 1.定义一个Converter 转换消息 2.添加fastjson配置信息, 比如 是否格式化数据 3.在Converter中添加配置信息
		 * 4.将Converter 添加到 List converters当中
		 */

		// 1.定义一个Converter 转换消息
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		// 2.添加fastjson配置信息, 比如 是否格式化数据
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		// 3.在Converter中添加配置信息
		fastConverter.setFastJsonConfig(fastJsonConfig);
		// 4.将Converter 添加到 List converters当中
		converters.add(fastConverter);

	}

	/**
	 * 注册拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 多个拦截器组成一个拦截器链
		// addPathPatterns 用于添加拦截规则
		// excludePathPatterns 用户排除拦截
		registry.addInterceptor(new DataInterceptor()).addPathPatterns("/**");
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		
		// 将所有/** 访问都映射到    指定文件目录 下
		registry.addResourceHandler("/**")
				.addResourceLocations("classpath:/static/");
		
	}

	


}
